#include "sprite.h"

#define IMAGE_NAME "hero2.png"

void createSprite(Sprite** s, SDL_Renderer *render, int x, int y){
    *s = malloc(sizeof(Sprite));
    strcpy((*s)->imgName, IMAGE_NAME);
    (*s)->posX = x;
    (*s)->posY = y;

    SDL_Surface * surface = IMG_Load((*s)->imgName);
    if(surface == NULL){
         SDL_Log("create surface  error : %s", SDL_GetError());
    }
    (*s)->texture = SDL_CreateTextureFromSurface(render, surface);
    SDL_FreeSurface(surface);
}

Sprite* createSprite2(SDL_Renderer *render){
    Sprite* s = malloc(sizeof(Sprite));
    strcpy(s->imgName, IMAGE_NAME);
    s->posX = 0;
    s->posY = 0;

    SDL_Surface * surface = IMG_Load(s->imgName);
    if(surface == NULL){
         SDL_Log("create surface  error : %s", SDL_GetError());
    }
    s->texture = SDL_CreateTextureFromSurface(render, surface);
    SDL_FreeSurface(surface);

    return s;
}

void showSprite(Sprite* this, SDL_Renderer *render){
    int width;
    int height;
    SDL_QueryTexture(this->texture, NULL, NULL, &width, &height);
    SDL_Rect dstRect = {this->posX, this->posY, width, height};

    SDL_RenderCopy(render, this->texture, NULL, &dstRect);
}

void destroySprite(Sprite* this){
   if(this != NULL){
      free(this);
      this = NULL;
   }
}